package com.xt.purchasingsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xt.purchasingsystem.annotation.PurchaseCache;
import com.xt.purchasingsystem.annotation.PurchaseClearCache;
import com.xt.purchasingsystem.dto.SysMenusDto;
import com.xt.purchasingsystem.model.SysMenus;
import com.xt.purchasingsystem.service.SysMenusService;
import com.xt.purchasingsystem.utils.IdWorker;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 菜单信息 前端控制器
 * </p>
 *
 * @author yhp
 * @since 2020-05-18
 */
@RestController
@RequestMapping("/menus")
@Api(description = "菜单管理API")
public class SysMenusController {
    @Autowired
    private SysMenusService sysMenusService;
    @Autowired
    private IdWorker idWorker;

    /**
     * 查询所有可用的菜单
     *
     * @return
     */
    @PurchaseCache(prefix = "menu:menus:", timeout = 7200, random = 1000)
    @ApiOperation(value = "菜单全部列表", notes = "菜单全部列表")
    @GetMapping("/queryAllMenu")
    public Result queryAllMenu() {
        return sysMenusService.queryAllMenus();
    }

    @PurchaseClearCache(prefix = "menu")
    @GetMapping("/del")
    public String del() {
       // redisUtil.delPrefixKey("menu:");
//        Set<String> keys = redisTemplate.keys("menu:*");
//        redisTemplate.delete(keys);
        return "删除缓存成功";
    }

    /**
     * 分页查询菜单的接口
     *
     * @param sysMenusDto
     * @return
     */
    @ApiOperation(value = "多条件分页查询菜单列表",notes = "多条件分页查询菜单列表")
    @PostMapping("/querySysMenusBySysMenusDto")
    @PurchaseCache(prefix = "menu:menus:", timeout = 7200, random = 1000)
    public Result querySysMenusBySysMenusDto(@RequestBody @ApiParam(name = "sysMenusDto", value = "查询条件", required = true) SysMenusDto sysMenusDto) {
        return sysMenusService.querySysMenusBySysMenusDto(sysMenusDto);
    }

    /**
     * 根据菜单编号修改菜单状态的接口
     *
     * @param id
     * @param status
     * @return
     */
    @ApiOperation(value = "根据菜单编号修改菜单状态的接口",notes = "根据菜单编号修改菜单状态的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "菜单ID", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "path", name = "status", value = "状态码,1为可用,0为不可用", required = true, dataType = "Integer"),
    })
    @PurchaseClearCache(prefix = "menu")
    @PutMapping("/updateMenurStatusById/{id}/{status}")
    public Result updateMenurStatusById(@PathVariable("id") String id,
                                        @PathVariable("status") Integer status) {
        return sysMenusService.updateMenurStatusById(id, status);
    }


    /**
     * 检查菜单是否被注册的接口
     *
     * @param menuName
     * @return
     */
    @ApiOperation(value = "检查菜单是否被注册的接口" ,notes = "检查菜单是否被注册的接口")
    @ApiImplicitParam(paramType = "path", name = "menuName", value = "菜单名称", required = true, dataType = "String")
    @PurchaseCache(prefix = "menu:name:", timeout = 7200, random = 1000)
    @GetMapping("/checkMenuName/{menuName}")
    public Result checkMenuName(@PathVariable("menuName") String menuName) {
        SysMenus sysMenus = sysMenusService.getOne(new QueryWrapper<SysMenus>().lambda().eq(SysMenus::getName, menuName));
        if (sysMenus != null) {
            return ResultFactory.buildSuccessResult("查询成功");
        } else {
            return ResultFactory.buildFailResult("查询失败");
        }
    }

    /**
     * 检查菜单链接地址是否被注册的接口
     *
     * @param linkUrl
     * @return
     */
    @ApiOperation(value = "检查菜单链接地址是否被注册的接口",notes = "检查菜单链接地址是否被注册的接口")
    @ApiImplicitParam(paramType = "path", name = "linkUrl", value = "菜单链接地址", required = true, dataType = "String")
    @PurchaseCache(prefix = "menu:linkUrl:", timeout = 7200, random = 100)
    @GetMapping("/checkMenuLinkUrl/{linkUrl}")
    public Result checkMenuLinkUrl(@PathVariable("linkUrl") String linkUrl) {
        SysMenus sysMenus = sysMenusService.getOne(new QueryWrapper<SysMenus>().lambda().eq(SysMenus::getLinkUrl, "/" + linkUrl));
        if (sysMenus != null) {
            return ResultFactory.buildSuccessResult("查询成功");
        } else {
            return ResultFactory.buildFailResult("查询失败");
        }
    }

    /**
     * 添加或修改菜单的接口
     *
     * @param sysMenus 菜单对象
     * @return
     */
    @ApiOperation(value = "添加或修改菜单的接口",notes = "添加或修改菜单的接口")
    @PostMapping("/savuOrUpdateMenu")
    @PurchaseClearCache(prefix = "menu")
    public Result savuOrUpdateMenu(@RequestBody @ApiParam(name = "sysMenus", value = "菜单对象", required = true) SysMenus sysMenus) {

        if (StringUtils.isEmpty(sysMenus.getId())) {
            sysMenus.setId(String.valueOf(idWorker.nextId()));
            boolean flag = sysMenusService.saveOrUpdate(sysMenus);
            if (flag) {
                return ResultFactory.buildSuccessResult("添加成功");
            } else {
                return ResultFactory.buildFailResult("添加失败");
            }
        } else {
            boolean flag = sysMenusService.saveOrUpdate(sysMenus);
            if (flag) {
                return ResultFactory.buildSuccessResult("修改成功");
            } else {
                return ResultFactory.buildFailResult("修改失败");
            }
        }

    }

    /**
     * 批量删除菜单的接口
     *
     * @param
     * @return
     */
    @ApiOperation(value = "批量删除菜单的接口",notes = "批量删除菜单的接口")
    @DeleteMapping("/delMenuByIds")
    @PurchaseClearCache(prefix = "menu")
    public Result delMenuById(@RequestBody @ApiParam(name = "ids", value = "菜单id集合", required = true) SysMenusDto sysMenusDto) {
        boolean flag = sysMenusService.removeByIds(sysMenusDto.getMenuIds());
        if (flag) {
            return ResultFactory.buildSuccessResult("删除成功");
        } else {
            return ResultFactory.buildFailResult("删除失败");
        }
    }
}

